#include<stdio.h>
#include<stdlib.h>

#include "portmapping.h"

int parse_portmapping_para(char * para,PortMapping * portmap)
{
	char *enable,*vname,*local_ip, *proctol, *protol_num, *external_port, *internal_port, *schedule;
	
	enable = strtok(para,",");

	if (!enable)
		return DM_FAILURE;
	
	printf("enable =%s\n",enable);
	strcpy(portmap->enable,enable);
	
	vname = strtok(NULL,",");
	if (!vname)
		return DM_FAILURE;
	
	printf("vname =%s\n",vname);
	strcpy(portmap->vname,vname);
	
	local_ip = strtok(NULL,",");
	if (!local_ip)
		return DM_FAILURE;
	printf("local_ip =%s\n",local_ip);
	strcpy(portmap->local_ip,local_ip);
	
	proctol = strtok(NULL,",");
	if (!proctol)
		return DM_FAILURE;
	printf("proctol =%s\n",proctol);
	
	strcpy(portmap->proctol,proctol);
	
	protol_num = strtok(NULL,",");
	if (!protol_num)
		return DM_FAILURE;
	
	printf("protol_num =%s\n",protol_num);
	strcpy(portmap->protol_num,protol_num);
	
	external_port = strtok(NULL,",");
	if (!external_port)
		return DM_FAILURE;
	
	printf("external_port =%s\n",external_port);
	strcpy(portmap->external_port,external_port);
	
	internal_port = strtok(NULL,",");
	if (!internal_port)
		return DM_FAILURE;

	printf("internal_port =%s\n",internal_port);
	strcpy(portmap->internal_port,internal_port);
	
	schedule = strtok(NULL,",");
	if (!schedule)
		return DM_FAILURE;
	
	printf("schedule =%s\n",schedule);
	strcpy(portmap->schedule,schedule);
	
}

int parse_portmapping_rule(char * rule,PortMapping * portmap)
{
	char *enable,*vname,*local_ip, *proctol, *protol_num, *external_port, *internal_port, *schedule;

	printf("aododbg---> rule = [%s]\n", rule);	
	enable = strsep(&rule,",");

	if (!enable)
		return DM_FAILURE;
	
	printf("enable =%s\n",enable);
	strcpy(portmap->enable,enable);
	
	vname = strsep(&rule,",");
	if (!vname)
		return DM_FAILURE;
	
	printf("vname =%s\n",vname);
	strcpy(portmap->vname,vname);
	
	local_ip = strsep(&rule,",");
	if (!local_ip)
		return DM_FAILURE;
	printf("local_ip =%s\n",local_ip);
	strcpy(portmap->local_ip,local_ip);
	
	proctol = strsep(&rule,",");
	if (!proctol)
		return DM_FAILURE;
	printf("proctol =%s\n",proctol);
	
	strcpy(portmap->proctol,proctol);
	
	protol_num = strsep(&rule,",");
	if (!protol_num)
		return DM_FAILURE;
	
	printf("protol_num =%s\n",protol_num);
	strcpy(portmap->protol_num,protol_num);
	
	external_port = strsep(&rule,",");
	if (!external_port)
		return DM_FAILURE;
	
	printf("external_port =%s\n",external_port);
	strcpy(portmap->external_port,external_port);
	
	internal_port = strsep(&rule,",");
	if (!internal_port)
		return DM_FAILURE;

	printf("internal_port =%s\n",internal_port);
	strcpy(portmap->internal_port,internal_port);
	
	schedule = strsep(&rule,",");
	if (!schedule)
		return DM_FAILURE;
	
	printf("schedule =%s\n",schedule);
	strcpy(portmap->schedule,schedule);
}

int tr69c_get_portmapping_names(char *rules, char (*names)[][64], int num)
{
	int i = 0;
	char *token1 = NULL, *token2 = NULL;
	PortMapping pms[MAX_PM_NUM] = {0};
	int ret = -1;
	
	token1 = strsep(&rules, ";");
	while(NULL != token1)
	{
		printf("i = %d, token1=[%s]\n", i, token1);
#if 0
		token2 = strsep(&token1, ",");
		while(NULL != token2)
		{
			enable = strsep(&token1, ",");
			if(!enable)
				return ret;

			vname = strsep(&token1, ",");
			if(!vname)
				return ret;

			printf("vname = [%s]\n", vname);
			strcpy((*names)[i], vname);
		}
#endif
		parse_portmapping_rule(token1, &pms[i]);
		strcpy((*names)[i], pms[i].vname);
		token1 = strsep(&rules, ";");
		i++;
	}
}

void tr69c_parse_portmapping_rules(char *rules, char (*names)[][64], int num)
{
	int i = 0;
	char *token = NULL;
	PortMapping pms[MAX_PM_NUM] = {0};
	
	token = strsep(&rules, ";");
	while(NULL != token)
	{
		i++;
		printf("i = %d, token=[%s]\n", i, token);
		parse_portmapping_rule(token, &pms[i]);
		token = strsep(&rules, ";");
	}
}

void tr69c_parse_portmapping_rule()
{
	
}

void main()
{
	//char pm_rules[512] = "1,vs1,192.168.0.88,TCP,6,12345,12345,Always;1,vs2,192.168.0.88,UDP,17,12312,12312,Always";
	char pm_rules[512] = "1,vs1,192.168.0.88,,6,12345,12345,Always;1,vs2,192.168.0.88,UDP,,12312,12312,Always";
	char names[MAX_PM_NUM][64] = {0};
	int i = 0;

	//tr69c_parse_portmapping_rules(pm_rules, names, MAX_PM_NUM);
	tr69c_get_portmapping_names(pm_rules, names, MAX_PM_NUM);
	for(i = 0; i < MAX_PM_NUM; i++)
	{
		printf("names[%d]=[%s]\n", i, names[i]);
	}
	
}
